package cn.xiaolang.function.genericity;

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

interface computer<T extends Map<K, V>, K, V>{
	public void show(T t);
}
/**
 * 测试泛型的使用
 */
public class GenericityForClass implements computer<Map<String,Double>,String,Double>{

	@Override
	public void show(Map<String, Double> t) {
		t.put("泛型K",Double.valueOf(3.0));
	}

	/**
	 * 
	 * 根据类的Class对象获取一个实例化对象
	 */
	@SuppressWarnings("unchecked")
	public  <T> T getInstance(Class<T> c){
		try {
//			String simpleName = c.getSimpleName();
			//从dao配置文件中获取，名字为simpleName的全类路径名
//			String implementName = ResourceBundle.getBundle("dao").getString(simpleName);
			String implementName = c.getName();
			// 配置文件里面 value 实现类的类路径 注意 : 对象无参数构造必须提供
			return (T) Class.forName(implementName).newInstance();
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException("工厂加载实例失败...." + e);
		}
	}
	
	public static void main(String[] args) {
		GenericityForClass genericityForClass = new GenericityForClass();
		Map<String, Double> t = new HashMap<>();
		genericityForClass.show(t);
		for(Entry<String, Double> entry : t.entrySet()){
			System.out.println(entry.getKey()+"=="+entry.getValue());
		}
		
		
		GenericityForClass instance = genericityForClass.getInstance(GenericityForClass.class);
		System.out.println(instance);
	}
	
	
}
